package com.zjsru.oneDay;

import java.util.HashMap;
import java.util.Map;

/**
 *罗马数字
 * 字符          数值
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 *
 *
 * 输入: s = "III"
 * 输出: 3
 *
 * 输入: s = "MCMXCIV"
 * 输出: 1994
 * 解释: M = 1000, CM = 900, XC = 90, IV = 4.
 */
public class romanToInt {
    Map<Character,Integer> symbolValues = new HashMap<Character,Integer>() {{
        put('I',1);

        put('V',5);

        put('X',10);

        put('L',50);

        put('C',100);

        put('D',500);

        put('M',1000);
    }};
    public int romanToInt(String s) {
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            int valus = symbolValues.get(s.charAt(i));
            if(i < n-1 && valus < symbolValues.get(s.charAt(i + 1))){
                ans -= valus;
            }else {
                ans += valus;
            }
        }
        return ans;
        }
    public static void main(String[] args){
        romanToInt romanToInt = new romanToInt();
        int result = romanToInt.romanToInt("MCMXCIV");
        System.out.println(result);
    }
}
